from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
from conan.tools.env import VirtualBuildEnv

class ErpcexConan(ConanFile):
    name = "erpcex"
    version = "1.7.3-patch"
    # Optional metadata
    license = "BSD-2-Clause"
    author = "guyadong 10km0811@sohu.com"
    url = "https://gitee.com/l0km/erpcex"
    description = "Extensive erpc base NXP Embedded RPC"
    topics = ("embedded","erpc","nxp")

    requires =  []
    package_type = "library"
    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False],"build_test": [True, False],
        "output_debug_log": [True, False],
        "concurrency_support": [True, False]}
    default_options = {"shared": False, "fPIC": True, "build_test": False, "output_debug_log": False, "concurrency_support": True}

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "erpc/*", "concurrency/*", "dual/*","test/*","cmake/config.cmake.in"

    def requirements(self):
        if self.options.concurrency_support or self.options.build_test:
            self.requires("common_source_cpp/1.0.1")
        # if self.settings.compiler == "msvc":
        #     self.requires("pthreads4w/3.0.0")

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC
            self.options.concurrency_support = False
            self.options.build_test = False
        if self.settings.compiler == "msvc":
            raise Exception("Unsupport Visual Studio,plese use MinGw compiler for Windows")

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["ERPCEX_CONCURRENCY_SUPPORT"] = self.options.concurrency_support
        tc.variables["ERPCEX_OUTPUT_DEBUG_LOG"] = self.options.output_debug_log
        tc.variables["ERPCEX_BUILD_TEST"] = self.options.build_test
        tc.generate()
    
        cd = CMakeDeps(self)
        cd.generate()

        env = VirtualBuildEnv(self)
        env.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_id(self):
        del self.info.options.build_test

    def package_info(self):
        # self.cpp_info.libs = ["erpc"]
        # self.cpp_info.includedirs = ["include","include/erpc"]
        # 指定将安装路径加入CMAKE_PREFIX_PATH,以便执行find_package时能扫描到安装路径
        self.cpp_info.builddirs.append(self.package_folder)
        # 抑制conan生成jpegwrapper-config.cmake使用jpegwrapper自己生成的cmake配置文件 
        self.cpp_info.set_property("cmake_find_mode", "none")